# -*- coding: utf-8 -*-
#import architect
from django.db import models
from django.contrib.auth.models import User as AuthUser
from django.conf import settings
from common.models import Site, BaseCategory, BaseEnum
# Create your models here.


class Category(BaseCategory):
    """
    分类
    """
    pass


class ArticleStatus(BaseEnum):
    """
    文章状态
    """
    Publish = 'publish'
    Draft = 'draft'


ARTICLE_STATUS = ArticleStatus.to_choices()


#@architect.install('partition', type='range', subtype='date', constraint='year', column='post_at')
class Article(models.Model):
    """
    文章
    """
    title = models.CharField(max_length=1024, verbose_name='标题')
    summary = models.CharField(max_length=1024, verbose_name='摘要')
    slug = models.SlugField(max_length=1024, verbose_name='slug')
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL, models.CASCADE, verbose_name='作者')
    categories = models.ManyToManyField(
        Category, related_name='c_articles', verbose_name='分类')
    tags = models.ManyToManyField(
        Category, related_name='t_articles', verbose_name='标签')
    status = models.CharField(
        max_length=32, choices=ARTICLE_STATUS, default='draft', verbose_name='状态')
    post_at = models.DateTimeField(verbose_name='发布时间')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')


class ArticleInputFormat(BaseEnum):
    """
    文章输入内容类型
    """
    Markdown = 'markdown'
    Html = 'html'


ARTICLE_INPUT_FORMAT = ArticleInputFormat.to_choices()


class ArticleContent(models.Model):
    """
    文章内容
    """
    article = models.OneToOneField(
        Article, models.CASCADE, primary_key=True, verbose_name='文章')
    display_content = models.TextField(verbose_name='显示内容')
    input_content = models.TextField(verbose_name='输入内容')
    input_format = models.CharField(
        max_length=32, choices=ARTICLE_INPUT_FORMAT, default='html', verbose_name='输入格式')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='修改时间')


class ArticleSource(models.Model):
    """
    文章来源信息
    """
    article = models.OneToOneField(
        Article, models.CASCADE, primary_key=True, verbose_name='文章')
    site = models.ForeignKey(Site, models.CASCADE, verbose_name='站点')
    url = models.URLField(max_length=1024, verbose_name='来源地址')
    object_key_int = models.IntegerField(default=0, verbose_name='int主键')
    object_key_str = models.CharField(
        max_length=1024, default='', verbose_name='str主键')
    content = models.TextField(verbose_name='内容')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
